using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomManager : MonoBehaviour
{
    public bool isboss;
    public int waveNeed;
    public GameObject[] enemyPrefab;
    public Transform[] enemyPos;
    public GameObject door;
    public GameObject chestPrefab;
    public GameObject tishiPrefab;
    public Dictionary<GameObject, Transform> dictionary;
    public int wave=0;
    private bool canCreate = false;
    private Collider2D coll,collplayer;
    private GameObject[] enemy;
    // Start is called before the first frame update
    void Start()
    {
        coll = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (collplayer.GetComponent<PlayerCon>().parameterPlayer.killNum == 0 && wave != 0)
        {
            if (wave != waveNeed + 1)
            {
                canCreate = true;
            }
            else if (wave == waveNeed + 1)
            {
                door.SetActive(false);
                GameObject chest = Instantiate(chestPrefab, enemyPos[0].position, enemyPos[0].rotation);
                Destroy(gameObject);
            }
        }
        if (canCreate)
        {
            collplayer.GetComponent<PlayerCon>().parameterPlayer.killNum = -enemyPrefab.Length;
            canCreate = false;
            wave++;
            if (isboss)
            {
                Create();
            }
            else
            {
                Invoke("Tishi", 2/3f);
                Invoke("Create", 2f);
            }
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            coll.enabled = false;
            collplayer = collision;
            wave = 1;
            canCreate = true;
            door.SetActive(true);
        }
    }
    void Create()
    {
        for (int i = 0; i < enemyPos.Length; i++)
        {
            GameObject enemy = Instantiate(enemyPrefab[i], enemyPos[i].position, enemyPos[i].rotation);
        }
    }
    void Tishi()
    {
        for (int i = 0; i < enemyPos.Length; i++)
        {
            GameObject tishi = Instantiate(tishiPrefab, enemyPos[i].position, enemyPos[i].rotation);
        }
    }
}
